/**
 * <AiRmob-Framework; A simple augmented reality framework for android >
    Copyright (C) <2012>  <AiRmob Team>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package rendercomponents;

import java.util.HashMap;
import java.util.Iterator;

import airmob.framework.AiRmobActivity;
import airmob.framework.CameraMatrizes;
import android.content.Context;
import android.graphics.Bitmap;
import android.opengl.GLSurfaceView;
import android.os.Handler;

public abstract class AiRmobRenderer implements GLSurfaceView.Renderer {
	public boolean surfaceCreatedDone = false;
	public int lastSynchFrameRate;
	public int lastRealFrameRate;
	public float cameraAngleHorizontal, cameraAngleVertical;
	public String rendererDescription = "No text entered!";
	Context mContext;
	public AiRmobRenderer(Context context){
		mContext = context;
	}
	public abstract boolean setMatrizes(CameraMatrizes cameraView);
	
	public abstract Object pick(int x, int y);
	public abstract void setVideoBackground(Bitmap bmp); 
	
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
///Functions for communication with framework 
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////
///Returns the picked Identifier as soon as it was picked
//////////////////////////////////////////////////////////

	private Handler messageHandler = AiRmobActivity.messageHandler;
	public AiRmobActivity.PickRunnable runnablePick;
	
	public void setPicked(final Object id){
			new Thread(){
				public void run(){	
					runnablePick.id = id;
					messageHandler.post(runnablePick);
				}
			}.start();
	}
	

	

	// informs the AiRmobActivity that the renderer has created its surface
	public AiRmobActivity.RendererCreatedRunnable runnableRendererCreated;
	
	
	public void rendererCreatedDone(){
		messageHandler.post(runnableRendererCreated);
	}
	
	// informs the AiRmobActivity that the renderer has changed its surface
		public AiRmobActivity.RendererChangedRunnable runnableRendererChanged;
		
		public void rendererChangedDone(){
			messageHandler.post(runnableRendererChanged);
		}
		
	
	// make objects available to AiRmobActivity
	protected HashMap<String, java.lang.Object> availableObjects = new HashMap<String, java.lang.Object>();
	
	
	/**
	 * Register the the given object with the given name. The object will then be accessible in an instance of AiRmobActivity.
	 * @param obj
	 * @param name
	 */
	public void registerObject(Object obj, String name){
		availableObjects.put(name, obj);
	}
	
	/**
	 * This method is usually called by an instance of AiRmobActivity and returns the object registered with the given name.
	 * @param name
	 * @return
	 */
	public java.lang.Object getObjectbyName(String name){		
		return availableObjects.get(name);		
	}
	
	public java.lang.Object[] getAllObjects(){
		java.lang.Object[] allObjs = new java.lang.Object[availableObjects.size()];
		String[] keys = new String[availableObjects.size()];
		Iterator iterator = availableObjects.keySet().iterator();
	    int i = 0;   
        while(iterator.hasNext()){        
            keys[i] = (String) iterator.next();
            i++;
        }
        for (i = 0; i < availableObjects.size(); i++){
        	allObjs[i] = availableObjects.get(keys[i]);
        }
		return allObjs;
	}
	
	
	public abstract boolean pickingActivated();
	
	public abstract void freezeCamera(boolean freeze);
	
}
